/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "file_文件名数据.h"
#include "file_载入保存数据.h"


#include "../字符串转换.h"




bool f_file_文件是否存在(const std::string& path) {
	return f_file_文件是否存在(std::filesystem::path(path));
}


bool f_file_文件是否存在(const std::filesystem::path& path) {
	std::error_code ec;
	bool 存在 = std::filesystem::exists(path.string(), ec);

	if (std::filesystem::is_regular_file(path)) {
		return true;
	}
	else {
		return false;
	}

	if (ec) {
		std::cout << ec.message() << "\n";
	}
	return 存在;
}

bool f_file_路径是否为目录(const std::string& path) {
	std::filesystem::path tmpPath(path);

	std::error_code ec;
	return std::filesystem::is_directory(tmpPath, ec);
}
bool f_file_路径是否为目录(const std::wstring& path) {
	std::filesystem::path tmpPath(path);

	std::error_code ec;
	return std::filesystem::is_directory(tmpPath, ec);
}

bool f_file_创建目录(std::string path) {
	if (!f_file_路径是否为目录(path)) {
		std::error_code ec;

		if (f_file_文件是否存在(path)) {
			path = f_file_get路径(path);
		}

		bool 是否创建成功 = std::filesystem::create_directories(path, ec);

		try {
			return 是否创建成功;
		}
		catch (std::filesystem::filesystem_error const& ex) {
			std::cout << "目录创建失败:" << path << std::endl;
		}
		return true;
	}
	return false;
}

bool f_file_创建目录(const std::filesystem::path& path) {
	return f_file_创建目录(path.u8string());
}


bool f_file_去路径空白字符(std::string& path) {
	uint32 num = path.size();
	for (uint32 i = 0; i < num; ) {
		if (i >= path.size()) break;
		if (path[i] == ' ') {
			path.erase(i, 1);
		}
		else {
			++i;
		}
	}
	return false;
}

std::string f_file_get文件名后缀(const std::string& path) {
	std::filesystem::path tmpPath(path);
	auto 后缀名称 = tmpPath.extension().string();
	std::transform(后缀名称.begin(), 后缀名称.end(), 后缀名称.begin(), [](uint8 c) { return std::tolower(c); });
	return 后缀名称;
}
std::u16string f_file_get文件名后缀(const std::u16string& path) {
	std::filesystem::path tmpPath(path);
	//auto 后缀 = tmpPath.stem().string();
	//std::transform(后缀.begin(), 后缀.end(), 后缀.begin(), ::tolower);
	return tmpPath.extension().u16string();
}

std::wstring f_file_取文件名(const std::wstring& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.filename();
}
std::u16string f_file_取文件名(const std::u16string& path) {
	std::filesystem::path tmpPath(path);

	return tmpPath.filename().u16string();
}
std::string f_file_get无后缀文件名(const std::string& path) {
	std::filesystem::path tmpPath(path);
	
	return tmpPath.stem().string();
}

std::wstring f_file_get无后缀文件名(const std::wstring& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.stem().wstring();
}
DEF_WString f_file_get无后缀文件名(const DEF_WString& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.stem().u16string();
}

std::wstring f_file_取宽字符无后缀文件名(const std::string& path) {
	std::filesystem::path tmpPath(path);
	return tmpPath.stem().wstring();
}

std::string f_file_替换后缀(const std::string& file, const std::string& 后缀) {
	std::string path = f_file_get路径(file) + f_file_get无后缀文件名(file);
	path += 后缀;
	return path;
}

std::string f_file_get路径(const std::string& fileName) {
	std::filesystem::path tmpPath(fileName);
	//tmpPath = tmpPath.parent_path();
	if (std::filesystem::is_directory(tmpPath) == false) {
		tmpPath = tmpPath.parent_path() / "";
	}
	return tmpPath.generic_string();
}

std::wstring f_file_get路径(const std::wstring& fileName) {
	std::filesystem::path tmpPath(fileName);
	if (std::filesystem::is_directory(tmpPath) == false) {
		tmpPath = tmpPath.parent_path() / "";
	}
	return tmpPath.generic_wstring();
}

DEF_WString f_file_get路径(const DEF_WString& fileName) {
	std::filesystem::path tmpPath(fileName);
	if (std::filesystem::is_directory(tmpPath) == false) {
		tmpPath = tmpPath.parent_path() / "";
	}
	return tmpPath.generic_u16string();
}

std::u16string f_file_补充文件路径后缀(const std::u16string& path, const std::u16string& 后缀) {
	auto a = f_file_get文件名后缀(path);
	if (a == 后缀) {
		return path;
	}
	std::u16string 新文件路径 = f_file_get路径(path) + f_file_get无后缀文件名(path) + 后缀;
	return 新文件路径;
}

std::string f_file_补充文件路径后缀(const std::string& path, const std::string& 后缀) {
	auto a = f_file_get文件名后缀(path);

	auto f = 后缀;
	if (f.front() != '.') {
		f.insert(0, ".");
	}
	if (a == f) {
		return path;
	}
	return f_file_get路径(path) + f_file_get无后缀文件名(path) + f;
}

int32 f_file_get目录下所有文件(const std::string& dir, std::vector<std::string>& 目录文件名, std::string 扩展名顾虑) {
	std::filesystem::path path(dir);
	if (!std::filesystem::exists(path)) {
		return 0;
	}

	目录文件名.clear();

	std::filesystem::directory_iterator end_iter;
	for (std::filesystem::directory_iterator iter(path); iter != end_iter; ++iter) {
		if (std::filesystem::is_regular_file(iter->status())) {
			
			目录文件名.push_back(iter->path().string());
		}

		if (std::filesystem::is_directory(iter->status())) {
			目录文件名.push_back(iter->path().string());
		}
	}
	return 目录文件名.size();
}

/*
file_type 枚举类

	type : file_type
		none
		not_found
		regular
		directory
		symlink
		block
		character
		fifo
		socket
*/

/*

	permissions : perms
		none
		owner_read
		owner_write
		owner_exec
		owner_all
		group_read
		group_write
		group_exec
		group_all
		others_read
		others_write
		others_exec
		others_all
		all
		set_uid
		set_gid
		sticky_bit
		mask
*/
bool f_file_get目录下所有文件名(const std::string& dir, S_目录文件包& 目录文件, std::string 扩展名过滤) {
	auto ec = std::error_code();
	//std::filesystem::file_status status(t);
	auto it = std::filesystem::directory_iterator(dir, ec);
	if (ec.value() == 5) return false;

	//std::cout << " ec = " << ec << std::endl;
	for (; it != std::filesystem::directory_iterator(); ++it) {
		if ((*it).is_directory()) {
			目录文件.目录.push_back(it->path().filename().u16string());
		}
		else if(it->is_regular_file()) {
			if (扩展名过滤.empty() || it->path().extension() == 扩展名过滤) {
				目录文件.文件.push_back(it->path().filename().u16string());
			}
		}
		//std::cout << " it->status().permissions() = " << uint32(it->status().permissions()) << std::endl;
	}
	return true;
}

bool f_file_get目录下所有文件名(const std::string& dir, S_目录文件包& 目录文件, const std::vector<std::string>& 扩展名过滤) {
	auto ec = std::error_code();
	//std::filesystem::file_status status(t);
	auto it = std::filesystem::directory_iterator(dir, ec);
	if (ec.value() == 5) return false;

	//std::cout << " ec = " << ec << std::endl;
	for (; it != std::filesystem::directory_iterator(); ++it) {
		if ((*it).is_directory()) {
			目录文件.目录.push_back(it->path().filename().u16string());
		}
		else if (it->is_regular_file()) {
			if (扩展名过滤.size()) {
				for (auto& 过滤 : 扩展名过滤) {
					if (it->path().extension() == 过滤) {
						目录文件.文件.push_back(it->path().filename().u16string());
					}
				}
			}
			else {
				目录文件.文件.push_back(it->path().filename().u16string());
			}
		}
	}

	return true;
}


bool f_file_get目录下所有文件名(const std::u16string& dir, S_目录文件包& 目录文件, const std::vector<std::u16string>& 扩展名过滤) {
	auto ec = std::error_code();
	auto it = std::filesystem::directory_iterator(dir, ec);
	if (ec.value() == 5) return false;

	for (; it != std::filesystem::directory_iterator(); ++it) {
		if ((*it).is_directory()) {
			目录文件.目录.push_back(it->path().filename().u16string());
		}
		else if (it->is_regular_file()) {
			if (扩展名过滤.size()) {
				for (auto& 过滤 : 扩展名过滤) {
					if (it->path().extension().u16string() == 过滤) {
						目录文件.文件.push_back(it->path().filename().u16string());
					}
				}
			}
			else {
				目录文件.文件.push_back(it->path().filename().u16string());
			}
		}
	}

	return true;
}


bool f_file_get目录下所有目录名(const std::string& dir, S_目录文件包& 目录文件) {
	auto ec = std::error_code();
	//std::filesystem::file_status status(t);
	auto it = std::filesystem::directory_iterator(dir, ec);
	if (ec.value() == 5) return false;

	for (; it != std::filesystem::directory_iterator(); ++it) {
		if ((*it).is_directory()) {
			目录文件.目录.push_back(it->path().filename().u16string());
		}
	}
	return true;
}

bool f_file_文件过滤(const std::u16string& file, const std::vector<std::u16string>& 扩展名过滤) {
	auto 后缀 = f_file_get文件名后缀(file);

	for (auto& e : 扩展名过滤) {
		if (e == 后缀) {
			return true;
		}
	}
	return false;
}





std::string f_file_读取字符串(const std::string& path) {
	std::string text;

	FILE* F = fopen(path.c_str(), "r");
	if (F) {
		uint32 size = 0;
		fread(&size, sizeof(uint32), 1, F);

		if (size) {
			text.resize(size);
			fread(text.data(), sizeof(int8), size, F);
		}
	}

	return text;
}



